package com.lmiot.floatviewsdklibrary.SDK;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.provider.Settings;
import android.view.WindowManager;
import com.lmiot.floatviewsdklibrary.View.FloatView;
import com.lmiot.floatviewsdklibrary.View.WrapFloatView;
import java.lang.reflect.Field;
import java.text.DecimalFormat;

public class YYFloatButton {
    private static WrapFloatView floatview = null;
    private static WindowManager.LayoutParams layoutParams = null;
    public static boolean mLongPress = false;
    public static OnDirectListener mOnDirectListener;
    public static OnDirectListenerRightTime mOnDirectListenerRightTime;
    private static WindowManager mWindowManager;
    private static WindowManager windowManager;

    public interface OnDirectListener {
        void result(FloatView.Mode mode);
    }

    public interface OnDirectListenerRightTime {
        void result(boolean z, FloatView.Mode mode);
    }

    public static void show(Context context) {
        if (!checkOp(context)) {
            gotoFloatPermissionSetting(context);
            return;
        }
        try {
            WindowManager windowManager2 = getWindowManager(context);
            windowManager = windowManager2;
            int width = windowManager2.getDefaultDisplay().getWidth();
            int height = windowManager.getDefaultDisplay().getHeight();
            if (floatview == null) {
                floatview = new WrapFloatView(context);
                if (layoutParams == null) {
                    layoutParams = new WindowManager.LayoutParams();
                    if (Build.VERSION.SDK_INT >= 26) {
                        layoutParams.type = 2038;
                    } else {
                        layoutParams.type = 2003;
                    }
                    layoutParams.format = 1;
                    layoutParams.flags = 40;
                    layoutParams.gravity = 51;
                    int floatViewSize = getFloatViewSize(context) * 500;
                    layoutParams.width = floatViewSize / 100;
                    layoutParams.height = floatViewSize / 100;
                    floatview.setAlpha(Float.parseFloat(new DecimalFormat("0.0").format((double) (((float) getFloatViewAlaph(context)) / 100.0f))));
                    int lastX = getLastX(context);
                    int lastY = getLastY(context);
                    if (lastX == 0 && lastY == 0) {
                        layoutParams.x = width;
                        layoutParams.y = height / 2;
                    } else {
                        layoutParams.x = lastX;
                        layoutParams.y = lastY;
                    }
                }
                floatview.setParams(layoutParams);
                windowManager.addView(floatview, layoutParams);
            }
        } catch (Exception e) {
            e.printStackTrace();
            hide();
            newFloatView(context);
        }
    }

    private static void newFloatView(Context context) {
        try {
            WindowManager windowManager2 = getWindowManager(context);
            windowManager = windowManager2;
            int width = windowManager2.getDefaultDisplay().getWidth();
            int height = windowManager.getDefaultDisplay().getHeight();
            if (floatview == null) {
                floatview = new WrapFloatView(context);
                if (layoutParams == null) {
                    layoutParams = new WindowManager.LayoutParams();
                    if (Build.VERSION.SDK_INT >= 26) {
                        layoutParams.type = 2038;
                    } else {
                        layoutParams.type = 2002;
                    }
                    layoutParams.format = 1;
                    layoutParams.flags = 40;
                    layoutParams.gravity = 51;
                    int floatViewSize = getFloatViewSize(context) * 500;
                    layoutParams.width = floatViewSize / 100;
                    layoutParams.height = floatViewSize / 100;
                    floatview.setAlpha(Float.parseFloat(new DecimalFormat("0.0").format((double) (((float) getFloatViewAlaph(context)) / 100.0f))));
                    int lastX = getLastX(context);
                    int lastY = getLastY(context);
                    if (lastX == 0 && lastY == 0) {
                        layoutParams.x = width;
                        layoutParams.y = height / 2;
                    } else {
                        layoutParams.x = lastX;
                        layoutParams.y = lastY;
                    }
                }
                floatview.setParams(layoutParams);
                windowManager.addView(floatview, layoutParams);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateSize(Context context, int i) {
        if (i <= 10) {
            i = 10;
        }
        if (i >= 100) {
            i = 100;
        }
        try {
            setFloatViewSize(context, i);
            try {
                int i2 = i * 500;
                layoutParams.width = i2 / 100;
                layoutParams.height = i2 / 100;
                mWindowManager.updateViewLayout(floatview, layoutParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    public static void updateAlaph(Context context, int i) {
        if (i <= 10) {
            i = 5;
        }
        if (i >= 100) {
            i = 100;
        }
        try {
            setFloatViewAlaph(context, i);
            try {
                floatview.setAlpha(Float.parseFloat(new DecimalFormat("0.0").format((double) (((float) i) / 100.0f))));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    public static void changeColor(Context context, int i) {
        try {
            setFloatViewColor(context, i);
            hide();
            show(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void hide() {
        try {
            WrapFloatView wrapFloatView = floatview;
            if (wrapFloatView != null) {
                WindowManager windowManager2 = windowManager;
                if (windowManager2 != null) {
                    windowManager2.removeView(wrapFloatView);
                }
                floatview = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean isShowing() {
        return floatview != null;
    }

    private static WindowManager getWindowManager(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }

    public static void setOnDirectListener(OnDirectListener onDirectListener) {
        mOnDirectListener = onDirectListener;
    }

    public static void setFloatMode(Context context, boolean z) {
        context.getSharedPreferences("FloatData_1", 0).edit().putBoolean("getFloatMode", z).commit();
    }

    public static boolean getFloatMode(Context context) {
        return context.getSharedPreferences("FloatData_1", 0).getBoolean("getFloatMode", false);
    }

    public static void setFloatViewSize(Context context, int i) {
        context.getSharedPreferences("FloatData_1", 0).edit().putInt("setFloatViewSize", i).commit();
    }

    public static int getFloatViewSize(Context context) {
        return context.getSharedPreferences("FloatData_1", 0).getInt("setFloatViewSize", 50);
    }

    public static void setFloatViewAlaph(Context context, int i) {
        context.getSharedPreferences("FloatData_1", 0).edit().putInt("setFloatViewAlaph", i).commit();
    }

    public static int getFloatViewAlaph(Context context) {
        return context.getSharedPreferences("FloatData_1", 0).getInt("setFloatViewAlaph", 80);
    }

    public static void setFloatViewColor(Context context, int i) {
        context.getSharedPreferences("FloatData_1", 0).edit().putInt("setFloatViewColorNew", i).commit();
    }

    public static int getFloatViewColor(Context context) {
        return context.getSharedPreferences("FloatData_1", 0).getInt("setFloatViewColorNew", 1);
    }

    public static void setLastX(Context context, int i) {
        context.getSharedPreferences("FloatData_1", 0).edit().putInt("setLastX", i).commit();
    }

    public static int getLastX(Context context) {
        return context.getSharedPreferences("FloatData_1", 0).getInt("setLastX", 0);
    }

    public static void setLastY(Context context, int i) {
        context.getSharedPreferences("FloatData_1", 0).edit().putInt("setLastY", i).commit();
    }

    public static int getLastY(Context context) {
        return context.getSharedPreferences("FloatData_1", 0).getInt("setLastY", 0);
    }

    public static void setmOnDirectListenerRightTime(OnDirectListenerRightTime onDirectListenerRightTime) {
        mOnDirectListenerRightTime = onDirectListenerRightTime;
    }

    public static boolean checkOp(Context context) {
        if (Build.VERSION.SDK_INT >= 23) {
            return Settings.canDrawOverlays(context);
        }
        boolean z = Build.VERSION.SDK_INT < 19;
        if (z || Build.VERSION.SDK_INT < 19) {
            return z;
        }
        if (((AppOpsManager) context.getSystemService("appops")) == null) {
            return true;
        }
        try {
            Class<?> cls = Class.forName("android.content.Context");
            Field declaredField = cls.getDeclaredField("APP_OPS_SERVICE");
            declaredField.setAccessible(true);
            Object obj = declaredField.get(cls);
            if (!(obj instanceof String)) {
                return false;
            }
            Object invoke = cls.getMethod("getSystemService", String.class).invoke(context, (String) obj);
            Class<?> cls2 = Class.forName("android.app.AppOpsManager");
            Field declaredField2 = cls2.getDeclaredField("MODE_ALLOWED");
            declaredField2.setAccessible(true);
            if (((Integer) cls2.getMethod("checkOp", Integer.TYPE, Integer.TYPE, String.class).invoke(invoke, 24, Integer.valueOf(Binder.getCallingUid()), context.getPackageName())).intValue() == declaredField2.getInt(cls2)) {
                return true;
            }
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static void gotoFloatPermissionSetting(Context context) {
        try {
            Intent intent = new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION");
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
